<?php

require_once( '../system/smarty.inc' );

require_once( '../system/tdao/Class_T_PATTERN.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_T_PATTERN,$login['staff_auth']);


$dosearch = FALSE ;

$dao = new Class_T_PATTERN;
$matrix_cd = $_GET['matrix_cd'] ;
$dao->setTableNameWeek($matrix_cd);

/*
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['INS_SHOP'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['INS_SHOP'] = $forms;
}

$area_zone_flg  = 0;
if   ($login['staff_auth'] == 'G-2') {
	$area_zone_flg = 2;
}
if   ($login['staff_auth'] == 'G-3') {
	$area_zone_flg = 3;
}
$wherearr =  array();
if ( isset( $forms['block_cd'] ) &&  ( !empty( $forms['block_cd'] ) ) ){
	$wherearr[] = sprintf(" c.block_cd =  '%s' ",  $forms['block_cd'] );
}

if ( isset( $forms['shop_cd'] ) &&  ( !empty( $forms['shop_cd'] ) ) ){
	$wherearr[] = sprintf(" c.shop_cd =  '%s' ",  $forms['shop_cd'] );
}

if ( isset( $forms['upd_f'] ) &&  ( !empty( $forms['upd_f'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_f'], 1, 'd' );
}

if ( isset( $forms['upd_t'] ) &&  ( !empty( $forms['upd_t'] ) ) ){
	$wherearr[] = Class_PWD::get_u_time_where_sql($forms['upd_t'],2, 'd' );
}
*/

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	
	$_GET["csv"] = "";
	$tmpfilename = "pattern_shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	
	$area_zone_flg  = 0;
	if   ($login['staff_auth'] == 'G-2') {
		$area_zone_flg = 2;
	}
	if   ($login['staff_auth'] == 'G-3') {
		$area_zone_flg = 3;
	}
	if   ($login['staff_auth'] == 'G-4') {
		$area_zone_flg = 4;
	}
	$patterns = $dao->getpatterns();
	$data     = $dao->getShops( $patterns, $area_zone_flg);
	$colums   = array();
	//列名
	//a.shop_cd, c.shop_name, g.user_name, max(d.u_time) as u_time
	//	$colums["name"]  = array('shop_cd','shop_name','u_time','block_name','user_name');
	//	$colums["head"]  = array('店コード','店名','変更日','ゾーン','ゾーンＡＪＡ');
		$col_heads = array(
			'matrix_cd'        => array( 'マトリックスID',  1),
			'year4'            => array( '年度',            1),
			'week_cd'          => array( '週',              1),
			'flier_date'       => array( '折込日',          1),
			'shop_cd'          => array( '店舗コード',  1),
			'shop_name'        => array( '店名',  1),
			'zone_cd'          => array( 'ゾーンAJAコード',  1),
			'zone_nm'          => array( 'ゾーンAJA名',  1),
			'area_cd'          => array( 'エリアAJAコード',  1),
			'area_nm'          => array( 'エリアAJA名',  1)
		);
	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	foreach ($patterns as $k => $v) {
		$colums["head"][] = $v["pattern_name"];
		$colums["name"][] = $v["col"];
		$colums["type"][$v["col"]] = 1;
	}

	$col_heads_last = array(
		'u_time'        => array( '更新日時',       3),
		'c_time'        => array( '登録日時',       3)
	);
	foreach ($col_heads_last as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
	}
		
	$week = Class_M_OTHER::get_by_matrix_cd( $matrix_cd );
	$week["flier_date"] =  Class_PWD::int8_to_dateD($week["flier_date"]);
	foreach ($data as $k=>$v){ 
		$data[$k]["matrix_cd"]   =  $matrix_cd;
		$data[$k]["year4"]       =  $week["year4"];
		$data[$k]["week_cd"]     =  $week["week_cd"];
		$data[$k]["flier_date"]  =  $week["flier_date"];
	}
	$dao->output_csv($data, $colums);
	exit;
	
}


function  output_csv($data, $colums )
{
	if ( $colums["head"] ) {
		echo  implode(',', $colums["head"])."<br>";
	} else {
		echo  implode(',', $colums["name"])."<br>";
	}
	
	
	foreach ($data as $k => $v) {
		$linearr  = array();
		foreach ($colums["name"]  as $ck => $cv) {
			$linearr[]= $v[$cv];
		}
		echo implode(',', $linearr)."<br>";
		//echo  mb_convert_encoding(implode(',', $linearr), "SJIS-win", "UTF-8")."<br>";
	}
	
}
//---CSVダウンロード　処理完了


$isG2_3 = FALSE;
if   ($login['staff_auth'] == 'G-2' || $login['staff_auth'] == 'G-3')  {
	$isG2_3 = TRUE;
}
$smarty->assign('isG2_3', $isG2_3 );

$isG6_7 = FALSE;
if   ($login['staff_auth'] == 'G-2' || $login['staff_auth'] == 'G-3' || $login['staff_auth'] == 'G-4')  {
	$isG6_7 = TRUE;
}
$smarty->assign('isG6_7', $isG6_7 );
/*
$isG4 = FALSE;
if   ($login['staff_auth'] == 'G-4') {
		$isG4 = TRUE;
}
$smarty->assign('isG4', $isG4 );
*/
//==================================周
if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}
$smarty->assign('year4',  Class_PWD::get_years());

$week = Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR, TRUE );

$smarty->assign('week', $week );
$smarty->assign('forms',  $forms);
//==================================周

$smarty->assign('zoneflg', $area_zone_flg );

$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
//$smarty->assign('pageing', $smarty->fetch('page.html') );
$smarty->display('tpattern_shop.html');

?>